# -*- coding: utf-8 -*-
"""
Created on 2019/11/5 16:56
@author: iris.wang
@file  :  tags.py
"""

import logging
from util.db_util import execute_query


# 查询各类标签主表中标签总数量(行业，产品，概念，公司）
def get_tags_count(table):
    try:
        results = execute_query("SELECT COUNT(*) FROM %s WHERE state=2;" % table)
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve tags count failed!")
        return None


# 查询事件主表中事件总数量
def get_event_count():
    try:
        results = execute_query("SELECT COUNT(*) FROM nlp_dict_event_type WHERE stat=2;")
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve event count failed!")
        return None


# 查询人物主表中人物总数量
def get_people_count():
    try:
        results = execute_query(
            "SELECT SUM(cnt) FROM(SELECT COUNT(*) as cnt FROM `famous_people` WHERE state=2 UNION ALL SELECT COUNT(*) as cnt FROM `new_actual_controller` WHERE state=2 UNION ALL SELECT COUNT(*) as cnt FROM `new_base_executive` WHERE state=2)AS countdata;")
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve people count failed!")
        return None


# 查询新增一条标签数据是否同步相关表（行业，产品，概念）
def get_db_info(table, param, tag_id, id):
    if param in ["name", "cru", "upu", "upt"]:
        res = execute_query(
            "SELECT %s FROM %s WHERE %s='%s' AND state=2 ORDER BY upt DESC;" % (param, table, tag_id, id))
    elif param == "code":
        res = execute_query(
            "SELECT samcd FROM %s WHERE %s='%s' AND state=2 ORDER BY upt DESC;" % (table, tag_id, id))
    elif param == "words":
        res = execute_query(
            "SELECT word FROM %s WHERE %s='%s' AND state=2 ORDER BY upt DESC;" % (table, tag_id, id))
    try:
        results = []
        for i in range(len(res)):
            results.append(res[i][0])
            results = list(set(results))
            results = [str(x) for x in results]
        return results
    except Exception as e:
        logging.exception("Retrieve tag db info failed!")
        return None


# 查询测试用户新增最新一条事件数据id
def get_event_type_id(name):
    try:
        res = execute_query("SELECT id FROM nlp_dict_event_type WHERE name='%s' AND stat=2 ORDER BY upt DESC;" % name)
        return res[0][0]
    except Exception as e:
        logging.exception("Retrieve latest people id failed!")
        return None


# 新增一条事件数据是否同步事件相关表
def get_event_info(param, name):
    if param in ["cru", "upu", "upt"]:
        res = execute_query(
            "SELECT %s FROM nlp_dict_event WHERE name='%s' AND state=2 ORDER BY upt DESC;" % (param, name))
    elif param == "rules":
        res = execute_query(
            "SELECT rule FROM nlp_dict_event WHERE name='%s' AND state=2 ORDER BY upt DESC;" % name)
    elif param == "eventCode":
        res = execute_query(
            "SELECT event_code FROM nlp_dict_event WHERE name='%s' AND state=2 ORDER BY upt DESC;" % name)
    try:
        results = []
        for i in range(len(res)):
            results.append(res[i][0])
            results = list(set(results))
            results = [str(x) for x in results]
        return results
    except Exception as e:
        logging.exception("Retrieve event info failed!")
        return None


# 查询测试用户新增最新一条人物数据id
def get_latest_people_id(table, name):
    try:
        res = execute_query(
            "SELECT id FROM %s WHERE name='%s' AND state=2 AND cru='automation_tester1' ORDER BY upt DESC;" % (
            table, name))
        return res[0][0]
    except Exception as e:
        logging.exception("Retrieve latest people id failed!")
        return None


# 查询新增一条人物数据入库人物主表
def get_people_info(table, param, name):
    res = execute_query(
        "SELECT %s FROM %s WHERE name='%s' AND state=2 ORDER BY upt DESC;" % (param, table, name))
    try:
        results = []
        for i in range(len(res)):
            results.append(res[i][0])
            results = list(set(results))
            results = [str(x) for x in results]
        return results
    except Exception as e:
        logging.exception("Retrieve people info failed!")
        return None
